{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#处理 科目四驾考宝典1590题 科目一驾校宝典1853题\n",
    "#先用word保存为docx "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import docx\n",
    "from docx import Document\n",
    "from docx.oxml import OxmlElement\n",
    "from docx.enum.dml import MSO_THEME_COLOR"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 把有图片的题都注释上[有图片]  \n",
    "# doc = Document(r'./OriginData/科目四驾考宝典1590题.docx')\n",
    "\n",
    "# image_paragraphs = []\n",
    "# for par in doc.paragraphs:\n",
    "#     if 'graphicData' in par._p.xml:\n",
    "#         par.text=par.text+\"[有图片]\"\n",
    "#         #image_paragraphs.append(par)\n",
    "#         #break\n",
    "# doc.save('科目四驾考宝典1590题_process_pic.docx') \n",
    "\n",
    "\n",
    "#因为有些题缺失答案   在上面处理的基础上手动添加了答案 存到了：科目四驾考宝典1590题_process_pic.docx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "#把全部文字弄成一个string\n",
    "doc_path = './科目四驾考宝典1590题_process_pic.docx'\n",
    "\n",
    "def extract_text_from_docx(doc_path):\n",
    "    doc = docx.Document(doc_path)\n",
    "\n",
    "    text = \"\"\n",
    "    for paragraph in doc.paragraphs:\n",
    "        text += paragraph.text + \"\\n\"\n",
    "\n",
    "    for table in doc.tables:\n",
    "        for row in table.rows:\n",
    "            for cell in row.cells:\n",
    "                for paragraph in cell.paragraphs:\n",
    "                    text += paragraph.text + \"\\n\"\n",
    "\n",
    "    return text\n",
    "\n",
    "# 提取文档中的所有文字\n",
    "all_text = extract_text_from_docx(doc_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "数量：1594\n"
     ]
    }
   ],
   "source": [
    "#正则\n",
    "\n",
    "import re\n",
    "import json\n",
    "\n",
    "\n",
    "pattern = r\"\\d+[、.](.+?)\\n(.*?)答案[：:]*(.+?)\\n\"\n",
    "#pattern = r\"(\\d+)[、.](.+?)\\n(.*?)/n*答案：(.+?)\\s\"\n",
    "matches = re.findall(pattern, all_text,re.DOTALL)\n",
    "\n",
    "data = []\n",
    "for match in matches:\n",
    "    question = match[0].strip()\n",
    "    if match[1].strip():\n",
    "        options = match[1].strip()\n",
    "    else :\n",
    "        options=\"A、正确\\nB、错误\"\n",
    "\n",
    "    if match[2].strip()==\"√\" or match[2].strip()==\"正确\":\n",
    "        answer=\"A\"\n",
    "    elif match[2].strip()==\"×\" or match[2].strip()==\"错误\" or match[2].strip()==\"错\":\n",
    "        answer=\"B\"\n",
    "    else:\n",
    "        answer = match[2].strip()\n",
    "\n",
    "    pic_pattern=r\"[有图片]\"\n",
    "    if re.findall(pic_pattern, question+options+answer,re.DOTALL):\n",
    "        pic_flag=True\n",
    "    else:\n",
    "        pic_flag=False\n",
    "\n",
    "    \n",
    "    answer_pattern=r\"[^a-zA-Z]\"\n",
    "    if re.findall(answer_pattern, answer,re.DOTALL):\n",
    "        explainnation = re.findall(r'[^a-zA-Z]+', answer)[0]\n",
    "        answer=answer[0]\n",
    "    else:\n",
    "        explainnation=\"\"\n",
    "\n",
    "    question_data = {\n",
    "        'question': question,\n",
    "        'choose': options,\n",
    "        'answer': answer,\n",
    "        'picture':pic_flag,\n",
    "        'explainnation':explainnation\n",
    "    }\n",
    "\n",
    "    data.append(question_data)\n",
    "\n",
    "print(\"数量：\"+str(len(data)))\n",
    "\n",
    "# 将数据保存为JSON文件\n",
    "with open('question_data.json', 'w', encoding='utf-8') as json_file:\n",
    "    json.dump(data, json_file, ensure_ascii=False, indent=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 过滤掉有图片的\n",
    "with open('question_data.json') as file:\n",
    "    data = json.load(file)\n",
    "filtered_data=[item for item in data if not item['picture']]\n",
    "for item in data:\n",
    "    del item[\"picture\"]\n",
    "\n",
    "# 将数据保存为JSON文件\n",
    "with open('科目四驾考宝典1590题.json', 'w', encoding='utf-8') as json_file:\n",
    "    json.dump(filtered_data, json_file, ensure_ascii=False, indent=4)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "LLM",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
